Welcome back. To write a CSV file,
you just need to write text strings that follow the comma-separated values format.
Here we have the basic structure.
You write a header line with field names separated by commas.
You iterate through your list of objects and for each one you generate a line of output.
I've got here is just going to print the output to the Output Window,
but it's in the CSV format.
As you can see, we have one header row with the three headers,
Name, Age, and Sport and then each line has the same structure.
There's somebody's name and then a comma,
there's an age and then another comma and then a sport.
Now, if we want to change this instead of
printing to the Output Window we want to write to a file,
we'll do our usual transformation where instead of print will use.write,
and we'll have to open the file and close it.
I've actually already got that code written out.
I'm going to switch to it. So, I've opened the file for writing.
I've assigned the file object to this variable name outfile.
Then I'm writing the header on lines
89 and with the row strings instead of printing them out,
I'm doing outfile.write and because we're writing to a file,
we have to explicitly put in line breaks that we want.
Instead of putting it in the Output Window,
it's now in this data file which is available until we reload the page.
Again, the key to outputting CSV format
is just generating a string that contains one line,
where that line has commas that are separating into the field names or the values.
There are two options that work well for generating a line with
the commas separating the values and a third one that I don't really recommend.
Shown here, we're using the first method that I do
like and actually the one I liked the best which is to use a format string.
It's easy to see with this format string,
and we're going to have three values because we have three pairs
of curly braces one, two, three.
It's easy to see that we've got a comma separating each of them.
Then the values are just going to get substituted in.
The first element John Aalberge The second element goes there,
that's the 31 and Cross Country Skiing being the third element.
The second possibility is to use the.join method.
You may recall the.split method for chopping up a string into component parts.
Its counterpart going the other way is the.join method.
I would write something like this row string equals comma.join of some values.
Those values are going to be the same values that we used here.
So, I can use this instead,
I'll comment out the old version.
I always find this a little confusing and I think many students do too.
You might think that the natural thing is to call the join method and pass
comma as a parameter like we did with.split as in comma as a parameter.
But here, we're seeing join as an operation that we do on
the comma object and we have to
pass in as values the things that are going to be joined together.
You just have to remember that that's the opposite order of what you might expect.
So, it's comma.join.
There's a couple other tricky things about the join operator.
I'll show them to you here.
First I'm going to get rid of our markings.
When I run this, I'm going to get an error.
That's actually two problems.
The first problem is the join is expecting two arguments and I've given it four.
So, it really wants a list of things,
not a bunch of different values.
So, I have to give it a list.
I can put all of these values into a list.
One other tricky part about it is that join wants to have a list of strings,
a sequence of strings.
Olympian square bracket one is the number 31.
It isn't a string, it's an integer.
So, we get an error that it expected a string,
but it actually got something different.
Sequence item one, that's Olympian square bracket one.
So, if I turn it into a string,
I'll finally have something that works.
Now, I get the same output that I was getting before.
So, this looks pretty complicated and you're
probably thinking why would anyone ever want to do this.
Well, if my values were all strings,
then I might be tempted to do it.
Now, I don't need to say STR of Olympian.
I can just say, let it be in square bracket one there and it'll work,
and the thing that really makes this attractive in
this situation is that I can just refer to Olympian,
which is already a sequence of strings.
It's a tuple with three strings in it.
I can call.join and pass that tuple of strings and I still
get this lovely compact code and the same output.
So, if you have a list of strings,
then this.join method might be pretty attractive.
If not and you're going to have to do any kind
of converting integers to strings or things like that,
and I think you're going to want the version on line 13 that uses the format string.
A third possibility is to just use string concatenation.
But it gets really hard to read it.
It also is still going to require us to convert the number to a string.
So, we're going to have something like this,
and I think I'm pretty unlikely to get it right the first time,
that I will have row string equals
Olympian square bracket zero plus a comma plus
Olympian square bracket one plus another comma plus Olympian square bracket two.
That might work, let's see.
Yeah, I got the right output again.
So, little hard to read.
My preference generally is to use the format string like on line 13,
unless I really have a sequence of
strings in which case I might be tempted to use the comma.join from line 12.
Now, suppose we had slightly different data,
where one of the event names now has a comma in it,
but not all of them do.
Here you can see that Cross Country Skiing,
we have the 15 kilometer is specified rather than
the a 100 kilometer and some of the other events don't have commas in them.
You may remember that one of the ways we can handle
this thing is with the advanced CSV format,
where we put all of the values in quotes.
So, this is one of the nice things about Python having both single quotes or
double quotes as a way of delimiting a string is if we wanted to have
double quotes as a character inside the string,
we can use the single quotes as the delimiter as I've done here on line eight.
With our format string,
this is not too bad.
We just have double quotes around each of the pairs
of curly braces and we're still going to substitute in where the curly braces are.
So, the value is going to be surrounded by the double quotes.
This is a situation where I really appreciate the.format method.
I wouldn't want to try this with.join and I
definitely wouldn't want to try it using concatenation with the plus sign.
You'll see that our outputs have those quotes around all of the values.
In particular, we've got quotes around
the whole Cross Country Skiing comma 15 kilometers.
So, there's a comma that's inside one of the values inside the double quotes and
the other commas are separating the different values.
To summarize, the overall structure for writing a CSV file is to write the header line,
that's what we did on lines 89,
then iterate through all of your objects
and for each of them you're going to write one line into the CSV file.
So, I'm creating the row string,
I'm writing it and then I'm tacking on the backslash in to indicate the new line.
We'll see you next time.